//
// Created by Lenovo on 2024/2/28.
//
#include<bits/stdc++.h>
using namespace std;
//岛屿的最大面积
class Solution {
public:
    
    bool st[51][51];
    int m,n;
    int maxCount=0,cnt;
    
    void bfs(int i,int j,vector<vector<int>>& grid){
        cnt=0;
        queue<pair<int,int>> q;
        pair<int,int> start;
        start.first=i;start.second=j;
        st[i][j]= true;
        cnt++;
        q.push(start);
        while(q.size()>0){
            pair<int,int> cur=q.front();q.pop();
            int x=cur.first,y=cur.second;
            if(x-1>=0 && !st[x-1][y] && grid[x-1][y]==1){
                st[x-1][y]= true;
                cnt++;
                q.push({x-1,y});
            }
            if(x+1<m && !st[x+1][y] && grid[x+1][y]==1){
                st[x+1][y]=true;
                cnt++;
                q.push({x+1,y});
            }
            if(y-1>=0 && !st[x][y-1] && grid[x][y-1]==1){
                st[x][y-1]=true;
                cnt++;
                q.push({x,y-1});
            }
            if(y+1<n && !st[x][y+1] && grid[x][y+1]==1){
                st[x][y+1]= true;
                cnt++;
                q.push({x,y+1});
            }
        }
        
    }
    
    
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        m=grid.size();
        n=grid[0].size();
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(!st[i][j] && grid[i][j]==1){
                    bfs(i,j,grid);
                    maxCount= max(cnt,maxCount);
                }
            }
        }
        return maxCount;
    }
};
